{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "int16 tmn(time, lat, lon)\n",
      "    missing_value: -32768.0\n",
      "    unit: 0.1 degree centigrade\n",
      "    long_name: monthly minimum temperature\n",
      "unlimited dimensions: \n",
      "current shape = (36, 4717, 7680)\n",
      "filling on, default _FillValue of -32767 used\n",
      "0.1 degree centigrade\n",
      "(36, 4717, 7680)\n",
      "--\n"
     ]
    }
   ],
   "source": [
    "# netcdf\n",
    "#\n",
    "import os\n",
    "import numpy as np\n",
    "import netCDF4 as nc\n",
    "\n",
    "fname = r\"E:\\sfxData\\NetCDF\\tmn_1961_1963.nc\"\n",
    "\n",
    "# 打开一个 *.nc 文件并查看其属性\n",
    "ds = nc.Dataset(fname)\n",
    "# ds = nc.Dataset(fname, 'r', format='NETCDF4')\n",
    "\n",
    "# print(ds)\n",
    "# print(ds.groups.keys())         # 组信息   - dict_keys([])\n",
    "# print(ds.dimensions.keys())     # 维度信息 - dict_keys(['lon', 'lat', 'time'])\n",
    "# print(ds.variables.keys())      # 变量信息 - dict_keys(['lon', 'lat', 'time', 'tmn'])\n",
    "\n",
    "print(ds.variables['tmn'])\n",
    "print(ds.variables['tmn'].unit)  # 查看降水的单位 - 0.1 degree centigrade\n",
    "print(ds.variables['tmn'].shape)  # 查看数据维度 - (36, 4717, 7680)\n",
    "\n",
    "print(ds.variables['tmn'][10, 100, 100])\n",
    "\n",
    "# 因此考虑建一个循环，每个循环分别将一年的降水值求和，\n",
    "# 并添加到列表里，最后转换成数组求均值。\n",
    "# M = []\n",
    "# for i in os.listdir(\"E:/pre\"):\n",
    "#     dataset = nc.Dataset('E:/pre/' + i, 'r', format='NETCDF4')\n",
    "#     M.append(dataset.variables['pre'][:].sum(axis=0))  # 一年降水值求和\n",
    "\n",
    "# M = np.array(M)\n",
    "# M = M.sum(axis=0) / 120  # 120 年平均\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
